﻿using Microsoft.EntityFrameworkCore;
using Store.Model.Models;
using Store.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Store.Dal
{
    public class CommodityDAL
    {
        private readonly StoreContext Db;
        public CommodityDAL(StoreContext _db)
        {
            Db = _db;
        }

        /// <summary>
        /// 查询  显示
        /// </summary>
        /// <param name="sort"></param>
        /// <param name="state"></param>
        /// <param name="name"></param>
        /// <param name="brand"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<CommunityModel> GetCommodityList(int sort, int state, string name, string brand, int page, int limit, out int total)
        {
            var query = from c in Db.Commodities
                        join t1 in Db.Typees on c.TypeId equals t1.TypeId
                        join t2 in Db.Typees on t1.Tcid equals t2.TypeId
                        where c.ComDelState==1
                        select new CommunityModel
                        {
                            CommodityId = c.CommodityId,
                            CommodityName = c.CommodityName,
                            CommodityImg = c.CommodityImg,
                            ArticleNumber = c.ArticleNumber,
                            CommodityPrice = c.CommodityPrice,
                            CommodityState = c.CommodityState,
                            Brand = c.Brand,
                            SsalealesVolume = c.SsalealesVolume,
                            BagEmail = c.BagEmail,
                            CreateTime = c.CreateTime,
                            ShState = c.ShState,
                            Url = c.Url,
                            Payment = c.Payment,
                            Supplier = c.Supplier,
                            Store = c.Store,
                            TjCommodity = c.TjCommodity,
                            Servicee = c.Servicee,
                            CommodityContent = c.CommodityContent,
                            T1 = t1.TypeId,
                            TN1 = t1.TypeName,
                            T2 = (int)t2.Tcid,
                            TN2 = t2.TypeName
                        };


            if (sort != 0)
            {//排序
                if (sort == 1)
                    query = query.OrderByDescending(s => s.CommodityPrice);
                else if (sort == 2)
                    query = query.OrderBy(s => s.CommodityPrice);
                else if (sort == 3)
                    query = query.OrderByDescending(s => s.SsalealesVolume);
                else if (sort == 4)
                    query = query.OrderBy(s => s.SsalealesVolume);
                else if (sort == 5)
                    query = query.OrderByDescending(s => s.CreateTime);
                else if (sort == 6)
                    query = query.OrderBy(s => s.CreateTime);
                else
                    query = query.OrderBy(s => s.CommodityId);
            }
            //状态查询
            if (state != 0)
            {
                if (state == 1)
                {
                    query = query.Where(s => s.CommodityState == 1);
                }
                if (state == 2)
                {
                    query = query.Where(s => s.CommodityState == 2);
                }
                //query = query.Where(s => s.CommodityState == state);
            }
            //文本框查询关键词-订单号、商品货号
            if (!string.IsNullOrEmpty(name))
            {
                query = query.Where(s => s.CommodityName.Contains(name) || s.ArticleNumber.Contains(name));
            }
            //文本框查询品牌
            if (!string.IsNullOrEmpty(brand))
            {
                query = query.Where(s => s.Brand == brand);
            }
            //获取总条数
            total = query.Count();
            //db.Configuration.ProxyCreationEnabled = false;
            //分页
            var list = query.Skip((page - 1) * limit)
                            .Take(limit)
                            .ToList();
            //传参
            return list;
        }

        /// <summary>
        /// 查看商品信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Commodity> GetCommodity(int id)
        {
            var query = from c in Db.Commodities
                        where c.CommodityId == id
                        select c;
            return query.ToList();
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="commodity"></param>
        /// <returns></returns>
        public int CommodityAdd(Commodity commodity)
        {
            Db.Commodities.Add(commodity);
            return Db.SaveChanges();
        }


        /// <summary>
        /// 修改商品信息
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public int UpdCommodity(Commodity c)
        {
            var data = Db.Commodities.Find(c.CommodityId);
            if (data != null)
            {
                data.CommodityName = c.CommodityName;
                data.CommodityImg = c.CommodityImg;
                data.ArticleNumber = c.ArticleNumber;
                data.CommodityPrice = c.CommodityPrice;
                data.Brand = c.Brand;
                data.SsalealesVolume = c.SsalealesVolume;
                data.BagEmail = c.BagEmail;
                data.CreateTime = c.CreateTime;
                data.ShState = c.ShState;
                data.Url = c.Url;
                data.Payment = c.Payment;
                data.Supplier = c.Supplier;
                data.Store = c.Store;
                data.TjCommodity = c.TjCommodity;
                data.Servicee = c.Servicee;
                data.CommodityContent = c.CommodityContent;
                data.TypeId = c.TypeId;

                return Db.SaveChanges();
            }
            return -1;
        }


        /// <summary>
        /// 修改商品状态
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public int UpdComState(Commodity c)
        {
            var data = Db.Commodities.Find(c.CommodityId);
            if(data!=null)
            {
                data.CommodityState = c.CommodityState;

                return Db.SaveChanges();
            }
            return -1;
        }



        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sid"></param>
        /// <returns></returns>
        public int CommodityDel(int sid)
        {
            var sche = Db.Commodities.Find(sid);
            if (sche != null)
            {
                //真删
                //Db.Commodities.Remove(sche);

                //假删
                sche.ComDelState = 0;
                return Db.SaveChanges();
            }
            return -1;
        }
        /// <summary>
        /// 批删
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int CommodityDelList(string[] ids)
        {
            var query = from s in Db.Commodities
                        where ids.Contains(s.CommodityId.ToString())
                        select s;
            if (query.Count() > 0)
            {
                //真删
                //Db.Commodities.RemoveRange(query.ToList());

                //假删
                foreach (var item in query)
                {
                    item.ComDelState = 0;
                }
                return Db.SaveChanges();
            }
            return -1;
        }
        /// <summary>
        /// 绑定品牌下拉框
        /// </summary>
        /// <returns></returns>
        public List<string> GetCommunityTypes()
        {
            var query = from s in Db.Commodities
                        select s.Brand;
            return query.ToList();
        }



    }
}
